package com.huluobo.a2307douyindemo.mine

import androidx.recyclerview.widget.GridLayoutManager
import com.huluobo.a2307douyindemo.R
import com.huluobo.a2307douyindemo.adapter.CollectAdapter
import com.huluobo.a2307douyindemo.databinding.FragmentCollectBinding
import com.huluobo.lib_base.mvvm.BaseFragment

class CollectFragment : BaseFragment<FragmentCollectBinding, MineViewModel>() {
    private lateinit var collectAdapter: CollectAdapter
    override fun initData() {
        vm.queryCollectVideosSuccess.observe(this) {
            collectAdapter.data.clear()
            collectAdapter.data.addAll(it)
            collectAdapter.notifyDataSetChanged()
        }

        vm.deleteCollectVideoSuccess.observe(this) {
            vm.queryCollectVideos()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.queryCollectVideos()
    }

    override fun initView() {
        binding.collectRv.layoutManager = GridLayoutManager(requireContext(), 2)
        collectAdapter = CollectAdapter()
        binding.collectRv.adapter = collectAdapter

        collectAdapter.addChildClickViewIds(R.id.item_collect_delete_iv)
        collectAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_collect_delete_iv -> {
                    vm.deleteCollect(collectAdapter.data[position])
                }
            }
        }
    }
}